﻿using Microsoft.EntityFrameworkCore;

namespace EMall.IdentityService.Menus
{
    public class MenuRepository : IdentityServiceRepository<Menu, int>, IMenuRepository
    {
        public MenuRepository(IdentityServiceDbContext dbContext) : base(dbContext)
        {
        } 
        public async Task<Menu?> GetByNameAsync(string name) => await GetQueryable().FirstOrDefaultAsync(o => o.Name.Equals(name));
        public async Task<List<Menu>> GetListByParentIdAsync(int parentId) => await GetQueryable().Where(o => o.ParentId == parentId).ToListAsync();
        public async Task<List<Menu>> GetListByParentCodeAsync(string parentCode) => await GetQueryable().Where(o => o.Code.StartsWith(parentCode)).ToListAsync();
        public async Task<Menu?> GetLastChildByParentIdAsync(int parentId)
        {
            return await GetQueryable().Where(o => o.ParentId == parentId).OrderBy(o => o.Code).LastOrDefaultAsync();
        }
    }
}
